package com.meng.viewpager_define_demo;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by Administrator on 2017/5/21.
 */

public class DefineViewPager extends ViewPager{
    public DefineViewPager(Context context) {
        super(context);
        init();
    }

    public DefineViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setOffscreenPageLimit(5);
    }

    @Override
    public void setOffscreenPageLimit(int limit) {
        super.setOffscreenPageLimit(Integer.MAX_VALUE);
    }

    private int mCurShowChildIndex;

    /**0~ 1080   0
     * 1080~2160 1
     * 2160 ~ 3240 2
     *
     */
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        final int position = l/getWidth();
        if(position != mCurShowChildIndex){
            mCurShowChildIndex = position;
        }
        View childAt = getChildAt(mCurShowChildIndex);
        if(childAt != null){
            childAt.layout(l,0,getWidth()+l,getHeight());
        }
        Log.d("DefineViewPager", "mCurShowChildIndex:" + mCurShowChildIndex);
    }

    public void setmCurShowChildIndex(int mCurShowChildIndex) {
        this.mCurShowChildIndex = mCurShowChildIndex;
    }

}
